$(function () {
    init();
    function init() {
        renderData();
        $(".searchBtn").on("tap", function () {
            var txt = $(".searchTxt").val();
            if (!$.trim(txt)) {
                console.log("非法的");
                return;
            }
            var ls = localStorage;
            var arr = (ls.getItem("letaoHistory") && JSON.parse(ls.getItem("letaoHistory"))) || [];
            for (var i = 0; i < arr.length; i++) {
                var element = arr[i];
                if (element == txt) {
                    arr.splice(i, 1);
                }
            }
            arr.unshift(txt);
            ls.setItem("letaoHistory", JSON.stringify(arr));
            location.href = "./searchList.html?key=" + txt;
        })

        $(".clearAll").on("tap", function (e) {
            localStorage.removeItem("letaoHistory");
            $(".empty_history").show();
            $(".has_history").hide();
        })

        $(".has_history ul").on("tap", ".removeItem", function (e) {
            var spanDom = e.target;
            var index = $(spanDom).parent().data("index");
            var ls = localStorage;
            var arr = (ls.getItem("letaoHistory") && JSON.parse(ls.getItem("letaoHistory"))) || [];
            arr.splice(index, 1);
            if (arr.length == 0) {
                $(".empty_history").show();
                $(".has_history").hide();
            }
            var str = "";
            for (var i = 0; i < arr.length; i++) {
                var element = arr[i];
                str += ' <li> <span>' + element + '</span> <a data-index=' + i + ' class="removeItem mui-pull-right" href="javascript:;"><span class="mui-icon mui-icon-closeempty"></span></a> </li>';
            }
            $(".has_history ul").html(str);
            ls.setItem("letaoHistory", JSON.stringify(arr));
        })

        $(".history_ul").on("tap", "li", function (e) {
            var txt = $(this).children("span").html();
            var ls = localStorage;
            var arr = (ls.getItem("letaoHistory") && JSON.parse(ls.getItem("letaoHistory"))) || [];
            for (var i = 0; i < arr.length; i++) {
                var element = arr[i];
                if (element == txt) {
                    arr.splice(i, 1);
                }
            }
            arr.unshift(txt);
            ls.setItem("letaoHistory", JSON.stringify(arr));
            location.href = "./searchList.html?key=" + txt;
        })
    }
    function renderData() {
        var ls = localStorage;
        var arr = (ls.getItem("letaoHistory") && JSON.parse(ls.getItem("letaoHistory"))) || [];
        var str = "";
        for (var i = 0; i < arr.length; i++) {
            var element = arr[i];
            str += ' <li><span>' + element + '</span> <a data-index=' + i + ' class="removeItem mui-pull-right" href="javascript:;"><span class="mui-icon mui-icon-closeempty"></span></a> </li>';
        }
        $(".has_history ul").html(str);
        if (arr.length > 0) {
            $(".empty_history").hide();
        }
        else {
            $(".has_history").hide();
        }
    }
})